package com.study.controller;

import com.alibaba.fastjson.JSON;
import com.study.model.Greeting;
import com.study.model.ModelP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class HelloController {
	@Autowired
	RestTemplate restTemplate;

	/**
	 * http://localhost:8080/hello?param=hyj
	 */
	@GetMapping("/hello")
	public String hello(@RequestParam(name = "param", defaultValue = "world") String param) {
		return String.format("hello...%s", param);
	}

	@GetMapping("/greeting")
	public Greeting greeting(@RequestParam(name = "param", defaultValue = "world") String param) {
		return new Greeting(1, param);
	}

//	@GetMapping("/hello2")
//	public String hello2(Model model) {
//		model.addAttribute("hello2", "hello2");
//		return "HelloIndex";
//	}

	@GetMapping("/hello3")
	public String hello3(ModelAndView modelAndView) {
		return "HelloIndex";
	}


//    @Autowired
//    RestTemplate restTemplate;

	/**
	 * 本地写的接口,调main项目中的两个post接口
	 * /agent/project/pushproj2
	 * 调
	 * /agent/project/pushUnChangeProj
	 * /agent/project/pushChangeProj
	 */
	@PostMapping("/pushproj2")
	public void pushproj2(@RequestBody List<ModelP> list) {
		StringBuilder sb = new StringBuilder("");
		String url = "https://innersaleworkapi.fang.com";
		String body = "";
//        JSONObject jsonObject = JSONObject.parseObject(body);
		for (ModelP model : list) {
			try {
				JSON.toJSONString(model);
				String s = restTemplate.postForObject(url + "/customer/api/customer/addCustomerBindRelation", model, String.class);
				System.out.println("model:" + JSON.toJSONString(model) + "out:" + s);

			} catch (Exception e) {
				e.printStackTrace();
				sb.append(model.getBid() + ",");
			}

		}

		System.out.println(sb.toString());
		System.out.println(sb.toString());
		System.out.println(sb.toString());
		System.out.println(666);

//		restTemplate.postForObject(url + "/agent/project/pushUnChangeProj", UnbindProjParamDto.class, String.class);
//		restTemplate.postForObject(url + "/agent/project/pushChangeProj", BindProj400ParamDto.class, String.class);
	}


}
